import React from 'react';
import {
	Link,
} from 'react-router-dom'
import $ from 'jquery';

import Navigator from '../common/navigator';

class User extends React.Component {
	constructor(props){
		super();
		this.state = {
			id: props.match.params.id,
			data: [],
			total: '0',
			user:null,
			login:null,
		}
	}
	componentDidMount() {
		this.isLogin(this);
	}
	componentWillReceiveProps(nextProps) {

	}

	shouldComponentUpdate(nextProps, nextState) {
		return nextProps;
	}

	componentWillUpdate(nextProps, nextState) {

	}

	componentDidUpdate(prevProps, prevState) {

	}

	componentWillUnmount() {

	}
	login(){
		/*let data = {u_name:'jacky',u_pwd:'111111'};
		console.log('login fetch...',data);*/
		fetch('http://localhost:4000/user/login?u_name=jacky&u_pwd=111111', {
			method: 'GET',
			mode: 'cors',
			cache: 'default',
			dataType: 'json',
			//data:JSON.stringify(data),
		}).then((response) => {
			console.log('response login',response);
			return response.json();
		}).then((json) => {
			console.log('login json',json);
			this.setState({user: json.data});
		}).catch((err)=>{
			console.log('err',err);
		})
	}
	isLogin(){
		window.fetch('http://localhost:4000/user/islogin', {
			method: 'GET',
			mode: 'no-cors',
			cache: 'default',
			dataType: 'json',
			credentials: 'include',
		}).then((response) => {
			console.log('response isLogin',response);
			window['aaa'] =response;
			console.log('response isLogin',response.text());
			return response.json();
		}).then((json) => {
			console.log('json====1',json);
			this.setState({user: json.data});
		}).catch((err)=>{
			console.log('err',err);
		});
		/*TODO 为什么在nextwork里明明都看到返回数据了，可就是取不到数据呢？*/
		/*$.ajax({
			url: 'http://localhost:4000/user/islogin',
			type: 'GET',
			dataType: 'json',
			cache: false,
			xhrFields: {
				withCredentials: true
			},
			success: function (d) {
				console.log("ajax",d);
			},
			error: function (xhr, status, err) {
				console.log(status);
			}
		});*/

	}

	render(){
		let user = this.state.user||{};
		console.log('===',user);
		return (
			<div>
				<div className={"userMenuBox"}>
					{user.id} {user.u_name}
					<Link to={"/order"} className="a1">我的订单</Link>
						<div className="a1" onClick={this.login.bind(this)}>登录</div>
				</div>
				<Navigator/>
			</div>
		)
	}
}
export default User;